package com.g.places.api.util;

import com.g.places.api.base.PlacesClient.OutputType;

/**
 * 
 * GPlace Utility represent useful method to performance fetching data from google places 
 * 
 * @author ahmad
 *
 */
public class GPlacesUtil {

	
	
	// Question mark as String useful to create URL 
		public static String QUESTION_MARK = "?";
		
		// Ampersand symbol as String useful to create URL
		public static String AMPERSAND_SYMBOL = "&";
		
		
		
	/**
	 * 
	 * building full URL (web service url ) by given base URL , output type and parameters  
	 * 
	 * @param baseUrl 
	 * 				string represent base URL 
	 * @param outType
	 * 				output type either JSON or XML
	 * @param parameters
	 * 				adding request parameters to URL 
	 * @return string represent URL
	 */
	public static synchronized String defaultGPlacesUrlBuilder(String baseUrl,
			OutputType outType, Parameter... parameters) {

		// create url with given baseURL
		StringBuilder url = new StringBuilder(baseUrl);
		
		// add output type 
		url.append(outType.name().toLowerCase());
		
		// add question mark
		url.append(QUESTION_MARK);

		int i = 0;
		// adding request parameters
		for (Parameter parameter : parameters) {

			if (URLValidatorUtil.validParameter(parameter)) {
				// adding parameter
				url.append(parameter);
				
				// adding '&' between parameters
				if (i != parameters.length - 1) {
					url.append(AMPERSAND_SYMBOL);
				}
				
				i++;

			}

		}

		return url.toString();
	}
}
